import {deleteAction, getAction} from "@api/manage";
import utils from "@/utils/utils";

export const TableListMixin = {
    data() {
        return {
            dataSource: [],
            pagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                    return range[0] + '-' + range[1] + ' 共' + total + '条'
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
            sorter:{
                column: 'createTime',
                order: 'desc'
            },
            loading: false,
            selectedRowKeys: [],
            selectionRows: [],
            queryParam: {}, // 查询参数
            filters: {},
        }
    },
    mounted() {
        this.loadData(1);
    },
    methods: {

        // 加载数据方法
        loadData(args){
            if (!this.url.list) {
                this.$message.error('请设置url.list属性!')
                return
            }
            //加载数据 若传入参数1则加载第一页的内容
            if (args === 1) {
                this.pagination.current = 1
            }

            let params = this.getQueryParams()//查询条件
            this.loading = true
            getAction(this.url.list, params).then((res) => {
                console.info('res',res);
                if (res.code===200) {
                    this.dataSource = res.result.content
                    this.pagination.total = res.result.totalElements

                }
                this.loading= false
            })
        },

        getQueryParams() {
            //获取查询条件
            let sqp = {}
            if (this.superQueryParams) {
                sqp['superQueryParams'] = encodeURI(this.superQueryParams)
                sqp['superQueryMatchType'] = this.superQueryMatchType
            }
            let param = Object.assign(sqp, this.queryParam, this.sorter, this.filters)
            param.pageNo = this.pagination.current
            param.pageSize = this.pagination.pageSize
            return utils.filterObj(param);
        },


        // 列表改变事件方法
        handleTableChange(pagination, filters, sorter){
            if(Object.keys(sorter).length > 0){
                this.sorter.column = sorter.field;
                console.info(sorter);
                this.sorter.order = "ascend" === sorter.order ? "asc" : "desc";
            }
            this.pagination = pagination;
            this.loadData();
        },

        // 列表选择事件方法
        onSelectChange(selectedRowKeys,selectionRows){
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },

        // 打开新增弹框
        handleAdd: function () {
            this.$refs.modalForm.add();
            this.$refs.modalForm.title = "新增";
            this.$refs.modalForm.action = 'add';
            this.$refs.modalForm.disableSubmit = false;
        },
        handleEdit: function (record){
            this.$refs.modalForm.edit(record);
            this.$refs.modalForm.title = "编辑";
            this.$refs.modalForm.action = 'edit';
            this.$refs.modalForm.disableSubmit = false;
        },
        handleDel: function (record){
            if(!this.url.delete){
                this.$message.error("请设置url.delete属性!")
                return
            }
            let that = this;
            deleteAction(that.url.delete, {id: record.id})
                .then((res) => {
                if (res.success) {
                    that.$message.success(res.message);
                    that.loadData();
                } else {
                    that.$message.warning(res.message);
                }
            });
        },

        modalFormOk(){
            this.loadData(1);
        }
    }
}